#!/data/Software/mydan/perl/bin/perl -I/data/Software/mydan/JOB/lib -I/data/Software/mydan/JOB/private/lib
use strict;
use warnings;
use MIME::Base64;
use YAML::XS;
use Code;
use Logs;

$| ++;

=head1 SYNOPSIS

    db => $mysql,
    id => 1,
    logs => 日志对象

=cut

return sub
{
    my %param = @_;
    my ( $db, $id, $logs ) = @param{qw(db id logs )};

    $logs = Logs->new( 'code.nodegroup' ) unless $logs;

    $logs->die( "id not a number" ) unless $id =~ /^\d+$/;
    my $x = eval{ $db->query( "select projectid,plugin,params from nodegroup where id='$id'" ) };
    $logs->die( "code.nodegroup get nodegroup info from mysql fail: $@" ) if $@;
    $logs->die( "code.nodegroup get nodegroup info from mysql fail" ) unless defined $x && ref $x eq 'ARRAY';
    $logs->die( "code.nodegroup id mismatch: id=$id" ) unless @$x;

    my ( $projectid, $plugin, $params ) = @{$x->[0]};
    
    $logs->die( "plugin format error" ) unless $plugin && $plugin =~ /^[a-z]+$/;
    my @node = eval{ Code->new( "nodegroup.plugin/$plugin" )->run( 
            id => $id, 
            db => $db,
            projectid => $projectid, 
            params => YAML::XS::Load decode_base64( $params ) 
        );
    };
    $logs->die( "code.nodegroup run nodegroup.plugin/$plugin fail: $@" ) if $@;
    return @node;
}

